# GitHub Actions workflow file.
#
# For more information, visit
# https://docs.github.com/actions/learn-github-actions.

---
name: pages

# Makes workflow run on push to the main or develop branches.
on:
  push:
    branches:
      - develop
      - main
  workflow_dispatch:
    inputs:
      debug:
        default: false
        description: Use Tmate session for debugging
        type: boolean

env:
  PIP_CACHE_DIR: "${{ github.workspace }}/.cache/pip"
  PIP_DISABLE_PIP_VERSION_CHECK: "true"
  PIP_ROOT_USER_ACTION: ignore
  POETRY_CACHE_DIR: "${{ github.workspace }}/.cache/pypoetry"
  POETRY_VIRTUALENVS_IN_PROJECT: "true"
  PYTHON_KEYRING_BACKEND: "keyring.backends.null.Keyring"

jobs:
  docs:
    name: Build and publish documentation
    permissions:
      contents: write
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code repository
        uses: actions/checkout@v4
      - name: Cache Python packages
        uses: actions/cache@v4
        with:
          path: |
            .cache
            .venv
          key: >-
            python-3.11_${{ runner.os }}_poetry-${{ hashFiles('poetry.lock') }}
      - name: Install Python interpreter
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"
      - name: Install Python packages
        run: |
          python --version
          python -m pip --version
          python -m pip install poetry
          poetry lock --check
          poetry install --verbose
      - name: Build documentation
        run: poetry run python scripts/build_docs.py
      - name: Publish documentation
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ github.token }}
          publish_dir: site
      - name: Setup Tmate session if debug enabled
        if: >-
          ${{ always() && github.event_name == 'workflow_dispatch' &&
          inputs.debug }}
        timeout-minutes: 60
        uses: mxschmitt/action-tmate@v3
